{
 "metadata": {
  "language": "Julia",
  "name": "",
  "signature": "sha256:f2abd7de2b545b6e0dd9a471db22d0ddd141ff763c5ea73d4db2034b3215b149"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "using Convex, Gadfly, SCS"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Start by reading in some files with the data\n",
      "\n",
      "# Lists of line and pixel indices\n",
      "line_indices = readdlm(\"line_indices.txt\");\n",
      "pixel_indices = readdlm(\"pixel_indices.txt\");\n",
      "# How much each pixel contributes to each line\n",
      "line_over_pixel = readdlm(\"line_over_pixel.txt\");\n",
      "# The above 3 files work together in the following manner:\n",
      "#   line #(line_indices[i]) crosses over pixel #(pixel_indices[i]) for distance (line_over_pixel[i])\n",
      "\n",
      "# Total whiteness covered by each line\n",
      "line_integral_values = readdlm(\"line_integral_values.txt\")/255;"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Form the sparse matrix from the data\n",
      "\n",
      "# Image is 50 x 50\n",
      "img_size = 50\n",
      "# The number of pixels in the image\n",
      "num_pixels = img_size * img_size;\n",
      "\n",
      "# line_mat will describe the geometry of the lines\n",
      "#   line_mat[i, j] denotes how much of pixel #j is cross over by line #i\n",
      "line_mat = spzeros(3300, num_pixels);\n",
      "num_vals = length(line_over_pixel);\n",
      "for i in 1:num_vals\n",
      "    xid = int(line_indices[i])\n",
      "    yid = int(pixel_indices[i])\n",
      "    line_mat[xid + 1, yid + 1] = line_over_pixel[i]\n",
      "end"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Setup the problem with Convex.jl\n",
      "\n",
      "# Variable for the color of each pixel\n",
      "pixel_colors = Variable(num_pixels);\n",
      "\n",
      "# line_mat * pixel_colors should be close to the line_integral_values\n",
      "#   to reflect that, we minimize a norm\n",
      "objective = norm(line_mat * pixel_colors - line_integral_values);\n",
      "\n",
      "# we want the pixels to have colors between 0 and 1\n",
      "constraints = [pixel_colors >= 0, pixel_colors <= 1];\n",
      "\n",
      "# build the problem\n",
      "problem = minimize(objective, constraints);"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "@time solve!(problem, SCSSolver(max_iters=10000, eps=1e-3));"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "# Let's look at if the values we got make sense\n",
      "max_pixel_color = maximum(pixel_colors.value)\n",
      "println(max_pixel_color)\n",
      "min_pixel_color = minimum(pixel_colors.value)\n",
      "println(min_pixel_color)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "# Plot the image using the pixel values obtained!\n",
      "rows = zeros(img_size*img_size)\n",
      "cols = zeros(img_size*img_size)\n",
      "for i = 1:img_size\n",
      "  for j = 1:img_size\n",
      "    rows[(i-1)*img_size + j] = i\n",
      "    cols[(i-1)*img_size + j] = img_size + 1 - j\n",
      "  end\n",
      "end\n",
      "\n",
      "color_values = (pixel_colors.value - min_pixel_color) / (max_pixel_color - min_pixel_color)\n",
      "p = plot(\n",
      "x=rows, y=cols, color=reshape(color_values, img_size, img_size), Geom.rectbin,\n",
      "    Scale.ContinuousColorScale(Scale.lab_gradient(color(\"black\"), color(\"white\")))\n",
      ")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}